package com.fanbai.score.user.fragemnt;

import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.fanbai.score.R;
import com.fanbai.score.common.Utils.ImageUtils;
import com.fanbai.score.common.Utils.ToastUtils;
import com.fanbai.score.common.activity.WebView_Activity;
import com.fanbai.score.common.base.BaseFragment;
import com.fanbai.score.common.http.CallBack;
import com.fanbai.score.user.activity.ShopDetails_Activity;
import com.fanbai.score.user.bean.ShopClass_Bean;
import com.fanbai.score.user.bean.ShopInfo_Bean;
import com.fanbai.score.user.http.UserProduct_RequestNet_Utils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by acer on 2017/5/13.
 */

public class Home_ElegantScore_Fragment extends BaseFragment{

    @BindView(R.id.view_recyclerView)
    RecyclerView mRecyclerView;

    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static Home_ElegantScore_Fragment newInstance(int sectionNumber) {
        Home_ElegantScore_Fragment fragment = new Home_ElegantScore_Fragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public Home_ElegantScore_Fragment() {
    }

    @Override
    protected int getLayoutID() {
        return R.layout.layout_recyclerview;
    }

    @Override
    public String getTitleName() {
        return null;
    }

    @Override
    public void iniTitleLeft(ImageView tv) {

    }

    @Override
    public void iniTitleRight(TextView tv) {

    }

    @Override
    protected void initView(View view) {
        mRecyclerView.addItemDecoration(new DividerItemDecoration(ctx,
                DividerItemDecoration.VERTICAL));
//        mRecyclerView.setNestedScrollingEnabled(false);
    }
    @Override
    protected void initData() {
        requestShopClass();
    }

    @Override
    protected void initListener() {

    }
    private void setClassListData(final List<ShopClass_Bean> beans){
//        mDatas = new ArrayList<>();
//        mDatas.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1494156320411&di=ee09c92ad8fa24ca4fe0efb54d902133&imgtype=0&src=http%3A%2F%2Fimg.newmotor.com.cn%2FUploadFiles%2Fimage%2F20160506%2F20160506114832563256.png");
//        mDatas.add("https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1494146013&di=8cdae46fd79675fb1e22e24572d7f578&src=http://www.moto2s.com/ziliao/59/2012102014411842848.jpg");
//        mDatas.add("https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1494146013&di=8cdae46fd79675fb1e22e24572d7f578&src=http://www.moto2s.com/ziliao/59/2012102014411842848.jpg");
        mRecyclerView.setLayoutManager(new LinearLayoutManager(ctx));
//        mRecyclerView.setNestedScrollingEnabled(false);
        CommonAdapter adapter = new CommonAdapter<ShopClass_Bean>(ctx, R.layout.item_imgview,
                beans){
            @Override
            protected void convert(ViewHolder holder, ShopClass_Bean s, int position) {
                ImageUtils.show(ctx, s.getClslogoimage(), (ImageView)holder.getView(R.id.itemImgView),  R.mipmap.ic_launcher);
            }
        };
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
//                ctx.jumpToActivity(ShopDetails_Activity.class);
                ruqestShopInfo(beans.get(position).getClsid());
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        mRecyclerView.setAdapter(adapter);
    }

    /**
     *获取一级分类
     */
    private void requestShopClass(){
        ctx.showDialog();
        UserProduct_RequestNet_Utils.getClassInfos(ctx, new CallBack<List<ShopClass_Bean>>() {
            @Override
            public void onSuccess(String code, String msg, List<ShopClass_Bean> beans) {
                ctx.hideDialog();
                setClassListData(beans);
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                ctx.hideDialog();
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                ctx.hideDialog();
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });
    }

    private void ruqestShopInfo(final String cid){
        ctx.showDialog();
        UserProduct_RequestNet_Utils.getShopInfosByCid(ctx, cid, new CallBack<List<ShopInfo_Bean>>() {
            @Override
            public void onSuccess(String code, String msg, List<ShopInfo_Bean> beans) {
                ctx.hideDialog();
                if(beans != null && beans.size() > 0){
                    Bundle bundle = new Bundle();
                    bundle.putString("url", "http://score.xinyou777.cn/customer/shop/shopinfo.php?sid="+beans.get(0).getSid());
                    ctx.jumpToActivity(WebView_Activity.class, bundle);
                }

            }

            @Override
            public void onFailure(String code, String msg, String body) {
                ctx.hideDialog();
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                ctx.hideDialog();
                ToastUtils.ErrorImageToast(ctx, msg);
            }
        });
    }
}
